$(function () {
    var videosecond = {};
    var videolength = $(".ui-video").length;
    if (videolength > 0) {
        $(".ui-video video").each(function () {
            var id = $(this).data("id");
            var video = $(this).get(0);
            var second = 16;
            videosecond[id] = second;//Lưu số giây toàn cục
            var duration;
            video.addEventListener("playing", function () {
                if (second > 0) {
                    $("#" + id).addClass("disabled");
                    duration = setInterval(function () {
                        second = second - 1;
                        videosecond[id] = second;
                        $("#" + id + " .Video_counter").text("(" + second + "s)");
                        if (second <= 0) {
                            $(".video.disabled").removeClass("disabled");
                            clearInterval(duration);
                            $("#" + id + ".video").text('Truy cập internet');
                        }
                    }, 1000);
                }
            });

            video.addEventListener("pause", function () {
                clearInterval(duration);
                $(".disabled").removeClass("disabled");
                if (videosecond[idVideoPlaying] <= 0) {
                    var video_link = $("#" + idVideoPlaying).prev().find('source').attr('src');
                    connectInternet(video_link);
                }
            });
        });
    }
    var idVideoPlaying = '';
    $(".target.video").on("tap", function (e) {
        var id = $(this).attr('id');
        idVideoPlaying = id;
        if (videosecond[id] <= 0) {
            var video_link = $(this).prev().find('source').attr('src');
            connectInternet(video_link);
        } else {
            $(this).addClass("disabled").prev()[0].play();
        }
        e.preventDefault();
    });

    function connectInternet(video_link) {
        $.ajax({
            type: "GET",
            url: baseurl + "campaign-video",
            timeout:5000,
            data: {
                video_link: video_link,
                mac: mac,
                nasid: nasid,
                name_device: name_device,
                id_campaign: id_campaign,
                landing_page: landing_page,
                id_place:id_place,
                type_time_session:type_time_session
            },
            success: function (data) {
                if (data == 200) {
                    $(".dialog-warning-text").empty();
                    Dialog.close();
                    Toast.showWait({text: "Truy cập internet...", width: "150px"});
                    location.href = authentication_link;
                }
            }
        });
    }
});



//                    function grabScreenshot() {
//                    ctx.drawImage(video, 0, 0, videoWidth, videoHeight);
//                            var img = new Image();
//                            img.src = canvas.toDataURL("image/png");
//                            img.width = 120;
//                            ssContainer.appendChild(img);
